package com.nuoniu.sibanyun.controller.erp;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.ErpPeriodDto;
import com.nuoniu.sibanyun.entity.dto.ErpPeriodSaveDto;
import com.nuoniu.sibanyun.entity.erp.ErpPeriod;
import com.nuoniu.sibanyun.entity.vo.ErpPeriodVo;
import com.nuoniu.sibanyun.service.erp.IErpPeriodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  会计期间 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-22
 */
@RestController
@RequestMapping("/erp/period")
@Api(value = "会计期间",tags = "会计期间")
public class ErpPeriodController extends BaseController {

    @Autowired
    private IErpPeriodService erpPeriodService;

    @GetMapping("/list")
    @ApiOperation(value = "获取当前 租户的 会计期间")
    public HttpResult<List<ErpPeriodVo>> list() {
        return HttpResult.ok(erpPeriodService.listErpPeriodVo());
    }


    @PostMapping("/save")
    @ApiOperation(value = "保存 会计期间")
    public HttpResult save(@RequestBody ErpPeriodSaveDto erpPeriodSaveDto) {
        erpPeriodService.saveErpPeriods(erpPeriodSaveDto);
        return HttpResult.ok();
    }

//    @DeleteMapping("/deleteLastYear")
//    @ApiOperation(value = "删除最后一年")
//    public HttpResult deleteByYear(Integer lastYear) {
//        erpPeriodService.deleteLastYear(lastYear);
//        return HttpResult.ok();
//    }

    @ApiOperation("财务期末记账")
    @GetMapping("settleAccounts")
    public HttpResult settleAccounts(Integer year, Integer month, Integer status) {
        erpPeriodService.settleAccounts(year, month, status);
        return HttpResult.ok();
    }

    @ApiOperation("年度结帐")
    @GetMapping("settleYearAccounts")
    public HttpResult settleYearAccounts() {
        erpPeriodService.settleYearAccounts();
        return HttpResult.ok();
    }

    @ApiOperation("进销存结帐")
    @GetMapping("save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份"),
            @ApiImplicitParam(name = "month", value = "月份"),
            @ApiImplicitParam(name = "status", value = "状态0反结 1结"),
            @ApiImplicitParam(name = "type", value = "类型1:采购2:销售3:库存"),
    })
    public HttpResult psiSettleAccounts(Integer year, Integer month, Integer status, Integer type) {
        if(type == null) {
            throw new NuoNiuException("类型不能为空");
        }

        erpPeriodService.psiSettleAccounts(year, month, status, type);
        return HttpResult.ok();
    }
}
